home *** CD-ROM | disk | FTP | other *** search
- Path: ts1-011.jaxnet.com!user
- From: garyg@jax.jaxnet.com (Gary M. Greenberg)
- Newsgroups: comp.lang.c
- Subject: Re: printf - adding commas to numbers
- Date: Fri, 12 Jan 1996 23:24:51 -0500
- Organization: Southeast Network Services, Inc.
- Message-ID: <garyg-1201962324510001@ts1-011.jaxnet.com>
- References: <4d15eq$lc5@ixnews2.ix.netcom.com> <4d3b5l$fsm@news.infi.net> <4d66rt$cc0@cloner3.netcom.com>
- NNTP-Posting-Host: ts1-011.jaxnet.com
-
- Chuck,
-
- It ain't printf as you requested; you could probably write a real simple
- function that counts the strlen that you want ","ed, and walk the string <of
- numbers> as they will surely be arrayable.
- Then, starting from the end of that array, "number[strlen(number)]",
- count down and insert a comma every third one ... here, an illustration
- ;-)
-
- /* commafy.c */
-
- /*
- ** function to insert ","s every third
- ** character for formatting numbers
- */
-
- #include <stdio.h>
- #include <string.h>
-
- #define STRLEN 120
-
- int main ( )
- {
- /*** simple test with hardwired input ***/
- char number[STRLEN]="987654321987654321";
- int i;
-
- for(i=strlen(number);i>0;i--) {
- putchar(number[strlen(number)-i]);
- if(i%3==1 && i>1 && i<=strlen(number))
- putchar(',');
- }
- return 0;
- }
-
-
- Not real elegant, _but_
-
- "It's tired and I'm getting late,
- "I want to try to sleep but I can't relate ..."
- -- SeaTrain.
-
- C'ya,
-
- gary /* the Sorcerer's Apprentice */
-
- "Why do we have to hide from the police, Daddy?" | Truth:
- "Because we use vi, honey. They use emacs." | This .sig is pirated
-